<script setup lang="ts">
import http from '@/utils/request'
// 修复导入路径和方式
import { reqLogin } from '../../api/user'
import { reactive } from 'vue'
import { User } from '@element-plus/icons-vue'
import { Lock } from '@element-plus/icons-vue'
import  useUserStore  from '../../store/modules/user.js'
import {ElMessage} from 'element-plus'
import { useRouter } from "vue-router";
let router = useRouter(); 

let userStore = useUserStore()

const state = reactive({
    username: '',
    password: ''
})

const login = async () => {
    // 调用用户仓库的登录方法
    let  a = await userStore.userLogin(state)
    if(a === 'ok'){
        router.push("/home");
        ElMessage.success("登录成功");
    }
}
// 修复：移除未定义的验证函数引用，使用Element Plus内置验证规则或定义自己的验证函数
const rules = reactive({
    username: [
        { required: true, message: '请输入用户名', trigger: 'blur' }
    ],
    password: [
        { required: true, message: '请输入密码', trigger: 'blur' },
        { min: 4, max: 9, message: '密码长度在6到20个字符', trigger: 'blur' }
    ]
});
</script>
<template>
    <div class="login-container">
      <div class="left">
        <!-- 装饰元素 -->
        <div class="decoration"></div>
        <div class="decoration"></div>
        
        <el-row>
          <el-col :span="12" :xs="0"></el-col>
          <el-col :span="12" :xs="24">
            <el-form class="login_form"
            :model="state"
            :rules="rules"
>
              <h1>Hello</h1>
              <h2>欢迎来到帅比网站</h2>
              <el-form-item label="用户名" prop="username">
                <el-input :prefix-icon="User" placeholder="请输入用户名" v-model="state.username" ></el-input>
              </el-form-item>
              <el-form-item label="密码" prop="password">
                <el-input :prefix-icon="Lock" type="password" placeholder="请输入密码" v-model="state.password" show-password></el-input>
              </el-form-item>
              <el-form-item>
                <el-button @click="login">登录</el-button>
              </el-form-item>
              <div class="footer">还没有账号？<a href="#" style="color: #6e45e2; text-decoration: none;">立即注册</a></div>
            </el-form>
          </el-col>
        </el-row>
      </div>
    </div>
  </template>
  <style scoped>
* {
    margin: 0;
    padding: 0;
    box-sizing: border-box;
    font-family: 'PingFang SC', 'Microsoft YaHei', sans-serif;
}

.login-container {
    min-height: 100vh;
    display: flex;
    justify-content: center;
    align-items: center;
    padding: 20px;
    background: linear-gradient(135deg, #6e45e2, #88d3ce);
}

.left {
    width: 100%;
    max-width: 1200px;
    height: 80vh;
    background: rgba(255, 255, 255, 0.15);
    backdrop-filter: blur(10px);
    border-radius: 20px;
    box-shadow: 0 15px 35px rgba(0, 0, 0, 0.2);
    overflow: hidden;
    position: relative;
    display: flex;
    align-items: center;
}

.left::before {
    content: '';
    position: absolute;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    background: url('https://images.unsplash.com/photo-1579546929662-711aa81148cf?ixlib=rb-1.2.1&auto=format&fit=crop&w=1350&q=80') no-repeat center center;
    background-size: cover;
    opacity: 0.7;
    z-index: -1;
}

.el-row {
    width: 100%;
    height: 100%;
}

.el-col {
    display: flex;
    align-items: center;
    justify-content: center;
}

.login_form {
    width: 80%;
    padding: 40px 30px;
    background: rgba(255, 255, 255, 0.95);
    border-radius: 16px;
    box-shadow: 0 10px 30px rgba(0, 0, 0, 0.1);
    text-align: center;
    transition: transform 0.3s ease, box-shadow 0.3s ease;
}

.login_form:hover {
    transform: translateY(-5px);
    box-shadow: 0 15px 35px rgba(0, 0, 0, 0.15);
}

h1 {
    color: #6e45e2;
    font-size: 2.5rem;
    margin-bottom: 10px;
    font-weight: 700;
}

h2 {
    color: #555;
    font-size: 1.3rem;
    margin-bottom: 30px;
    font-weight: 500;
}

:deep(.el-form-item) {
    margin-bottom: 25px;
}

:deep(.el-input) {
    border-radius: 8px;
    overflow: hidden;
}

:deep(.el-input__inner) {
    padding: 12px 15px;
    font-size: 1rem;
}

:deep(.el-button) {
    width: 100%;
    padding: 12px;
    font-size: 1rem;
    background: linear-gradient(135deg, #6e45e2, #88d3ce);
    border: none;
    color: white;
    border-radius: 8px;
    cursor: pointer;
    transition: all 0.3s ease;
}

:deep(.el-button:hover) {
    transform: translateY(-2px);
    box-shadow: 0 5px 15px rgba(110, 69, 226, 0.4);
}

.footer {
    margin-top: 20px;
    color: #777;
    font-size: 0.9rem;
}

/* 响应式设计 */
@media (max-width: 768px) {
    .left {
        height: auto;
        min-height: 70vh;
    }

    .login_form {
        width: 90%;
        padding: 30px 20px;
        top: 50%;
        transform: translateY(-50%);
        position: relative;
    }

    h1 {
        font-size: 2rem;
    }

    h2 {
        font-size: 1.1rem;
    }
}

/* 添加一些动画效果 */
@keyframes fadeIn {
    from {
        opacity: 0;
        transform: translateY(20px);
    }

    to {
        opacity: 1;
        transform: translateY(0);
    }
}

.login_form {
    animation: fadeIn 0.8s ease-out;
}

/* 装饰元素 */
.decoration {
    position: absolute;
    width: 200px;
    height: 200px;
    border-radius: 50%;
    background: linear-gradient(135deg, rgba(110, 69, 226, 0.2), rgba(136, 211, 206, 0.2));
    z-index: -1;
}

.decoration:nth-child(1) {
    top: -50px;
    left: -50px;
    width: 150px;
    height: 150px;
}

.decoration:nth-child(2) {
    bottom: -30px;
    right: -30px;
    width: 120px;
    height: 120px;
}
</style>
  
  <style>
/* 全局样式，用于引入Element Plus */
@import url('https://unpkg.com/element-plus/dist/index.css');
</style>